<?php
	include_once('db/config.php');

	function addUserProduct($username, $name ,$brand, $model, $serialNo, $description, $amountInStock, $unitPrice, $fileName)
	{	
		$result = false;
		try
		{
			connectDb("tictocwatches");
			global $dbLink;
			$queryString = "call sp_AddUserProduct('".mysqli_real_escape_string($dbLink,$username)."','".mysqli_real_escape_string($dbLink,$name)."','".mysqli_real_escape_string($dbLink,$brand)."','".mysqli_real_escape_string($dbLink,$model)."','".mysqli_real_escape_string($dbLink,$serialNo)."','".mysqli_real_escape_string($dbLink,$description)."',".mysqli_real_escape_string($dbLink,$amountInStock).",".mysqli_real_escape_string($dbLink,$unitPrice).",'".mysqli_real_escape_string($dbLink,$fileName)."')";			

			$resultPointer = mysqli_query($dbLink,$queryString);
			
			if($resultPointer)
			{
				while($resultRow = mysqli_fetch_assoc($resultPointer))
				{
					$result = $resultRow["product_id"];
				}
				return $result;				
			}
			else
			{
				$queryError = mysqli_errno($dbLink);
				if($queryError!=0)
				{
					print_r($queryError);
				}
			}
		}
		catch(Exception $ex)
		{
			throw new Exception($ex->getMessage());
		}	
		return $result;
	}
	
	function getAllProducts()
	{	
		$result = false;
		try
		{
			connectDb("tictocwatches");
			global $dbLink;
			$queryString = "call sp_getAllProducts()";

			$resultPointer = mysqli_query($dbLink,$queryString);
			
			if($resultPointer)
			{
				$result = array();
				while($resultRow = mysqli_fetch_assoc($resultPointer))
				{
					array_push($result, $resultRow);
				}
				return $result;				
			}
			else
			{
				$queryError = mysqli_errno($dbLink);
				if($queryError!=0)
				{
					print_r($queryError);
				}
			}
		}
		catch(Exception $ex)
		{
			throw new Exception($ex->getMessage());
		}	
		return $result;
	}
		
?>